/*Test Distrubition Server. Copyright (C) 2007-2008 Cameron de Witte. (cameron.dewitte@gmail.com)
* 
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
* License as published by the Free Software Foundation, either version 3 of the License, or any later version. 
* 
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
* 
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public 
* License along with this program. If not, see http://www.gnu.org/licenses/.
*/

using System;
using System.IO;
using System.Net.Sockets;
using System.Xml;

namespace TestDistrubitionClient
{
    internal class Client
    {
        private static void Main()
        {
            TcpClient myclient;
            StreamReader streamReader;
            StreamWriter streamWriter;
            string server;
            string friendlyName;
            int port;

            //Obtain the settings (server port and ip)
            XmlDocument XMLDoc = new XmlDocument();
            XMLDoc.Load("config.xml");
            XmlNodeList XMLNodes = XMLDoc.GetElementsByTagName("Config");
            XmlElement elem = (XmlElement) XMLNodes[0];
            server = elem.GetElementsByTagName("ServerIP")[0].InnerText;
            port = Convert.ToInt32(elem.GetElementsByTagName("ServerPort")[0].InnerText);
            friendlyName = elem.GetElementsByTagName("FriendlyName")[0].InnerText;

            //try and connect to the server
            try
            {
                myclient = new TcpClient(server, port);
                NetworkStream networkStream = myclient.GetStream();
                streamReader = new StreamReader(networkStream);
                streamWriter = new StreamWriter(networkStream);
                Console.WriteLine("Connected to the server at '" + server + "'.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.Write("Failed to connect to the server at '" + server + "', Press any key to quit.");
                Console.ReadKey();
                return;
            }

            //send to the server our name (1 is the message id, the | is a seperator, that we use to seperate messageID)
            string DataToSend = "1|" + friendlyName + "|" + Environment.MachineName;

            Console.WriteLine("Sending: " + DataToSend);
            Console.WriteLine(SendToServer(DataToSend, streamWriter, streamReader));

            //wait for a key press, then quit.
            Console.ReadKey();
            //messageID 2 is the disconnect command
            SendToServer("2|" + Environment.MachineName, streamWriter, streamReader);
        }

        private static string SendToServer(string text, TextWriter streamWriter, TextReader streamReader)
        {
            try
            {
                streamWriter.WriteLine(text);
                streamWriter.Flush();

                return streamReader.ReadLine();
            }
            catch (IOException ex)
            {
                return "-1|FATALERROR: " + ex.Message;
            }
            catch (OutOfMemoryException ex)
            {
                return "-1|FATAL ERROR: " + ex.Message;
            }
        }
    }
}